package com.info.zhiduoduo.repository.dto.crowdsource;

import com.info.zhiduoduo.common.constants.Constants;
import com.info.zhiduoduo.common.constants.crowdsource.CrowdsourceUserType;
import com.info.zhiduoduo.common.constants.project.CalcAmountType;
import com.info.zhiduoduo.common.constants.project.ProjectType;
import com.info.zhiduoduo.common.utils.NumberUtil;
import com.info.zhiduoduo.repository.dto.company.CompanyDetailDto;
import com.info.zhiduoduo.repository.entity.company.Company;
import com.info.zhiduoduo.repository.entity.entrustmentproject.EntrustmentProjectItem;

import java.math.BigDecimal;

import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class CalcCrowdsourceItemAmountModel {

	/** 计算的项目金额类型, 取值于 {@link com.info.zhiduoduo.common.constants.project.CalcAmountType} */
	private int calcAmountType;
	private int compid;
	/** 大项目id */
	private int projectid;
	/** 众包子项目id */
	private int projectItemId;
	/** 项目类型 2:智众包 8:创客团-智众包 ; 参考 {@link ProjectType} */
	private int projectType;
	/** 用户id 众包自然人crowdsourceid 或 工作室userid */
	private int userid;
	/** 用户类型 1:众包自然人 2:众包工作室 */
	private int userType;
	/** 项目金额 */
	private BigDecimal projectAmount;
	/** 众包城市ID */
	private Integer crowdsourceCity;
	/** 智众包阶梯式费率 1:否 2:开启 */
	private int crowdsourceStepped;
	/** 基础税率 */
	private double baseTaxRate;
	/** 年度25万以下税率 */
	private double lowTaxRate;
	/** 年度25万-75万税率 */
	private double highTaxRate;
	/** 保证金比例 */
	private double depositRate;
	/** 是否开启保证金 1:否 2:是 */
	private int depositFlag;
	/** 众包项目合伙人佣金比例 */
	private double partnerBrokerageRate;

	/**
	 * @param projectid
	 * @param projectType
	 * @param partnerBrokerageRate
	 * @param company
	 * @param item
	 * @param calcAmountType       计算的项目金额类型, 取值于 {@link com.info.zhiduoduo.common.constants.project.CalcAmountType}
	 * @return
	 */
	public static CalcCrowdsourceItemAmountModel of(int projectid, int projectType, double partnerBrokerageRate, Company company,
	                                                CrowdsourceProjectItemModel item, int calcAmountType, int workType, Integer crowdsourceCity) {
		CalcCrowdsourceItemAmountModel model = new CalcCrowdsourceItemAmountModel();
		double baseTaxRate = workType == Constants.WORK_TYPE_1 ? company.getBaseTaxRate() : company.getBaseTaxRateOdd();
		model.setCalcAmountType(calcAmountType)
				.setCompid(company.getCompid())
				.setProjectid(projectid)
				.setProjectType(projectType)
				.setProjectItemId(NumberUtil.safeInt(item.getProjectItemId()))
				.setUserid(item.getUserid())
				.setUserType(item.getUserType())
				.setProjectAmount(item.getProjectAmount())
				.setCrowdsourceCity(crowdsourceCity)
				.setCrowdsourceStepped(company.getCrowdsourceStepped())
				.setBaseTaxRate(baseTaxRate)
				.setLowTaxRate(company.getLowTaxRate())
				.setHighTaxRate(company.getHighTaxRate())
				.setDepositRate(company.getDepositRate())
				.setDepositFlag(company.getDepositFlag())
				.setPartnerBrokerageRate(partnerBrokerageRate);
		return model;
	}

	/**
	 * 智众包导入对象转换
	 * @param partnerBrokerageRate
	 * @param company
	 * @return
	 */
	public static CalcCrowdsourceItemAmountModel of(double partnerBrokerageRate, Company company,
													int userId, int userType, BigDecimal projectAmount,int calcAmountType,
													int workType,Integer crowdsourceCity) {
		CalcCrowdsourceItemAmountModel model = new CalcCrowdsourceItemAmountModel();
		double baseTaxRate = workType == Constants.WORK_TYPE_1 ? company.getBaseTaxRate() : company.getBaseTaxRateOdd();
		model.setCalcAmountType(calcAmountType)
			 .setCompid(company.getCompid())
			 .setProjectid(0)
			 .setProjectType(ProjectType.CROWDSOURCE)
			 .setProjectItemId(0)
			 .setUserid(userId)
			 .setUserType(userType)
			 .setProjectAmount(projectAmount)
			 .setCrowdsourceCity(crowdsourceCity)
			 .setCrowdsourceStepped(company.getCrowdsourceStepped())
			 .setBaseTaxRate(baseTaxRate)
			 .setLowTaxRate(company.getLowTaxRate())
			 .setHighTaxRate(company.getHighTaxRate())
			 .setDepositRate(company.getDepositRate())
			 .setDepositFlag(company.getDepositFlag())
			 .setPartnerBrokerageRate(partnerBrokerageRate);
		return model;
	}

	/**
	 * @param partnerBrokerageRate
	 * @param company
	 * @param item
	 * @param calcAmountType       计算的项目金额类型, 取值于 {@link com.info.zhiduoduo.common.constants.project.CalcAmountType}
	 * @return
	 */
	public static CalcCrowdsourceItemAmountModel of(double partnerBrokerageRate, Company company, EntrustmentProjectItem item,
	                                                int calcAmountType) {
		CalcCrowdsourceItemAmountModel model = new CalcCrowdsourceItemAmountModel();
		model.setCalcAmountType(calcAmountType)
				.setCompid(company.getCompid())
				.setProjectid(NumberUtil.safeInt(item.getImportId()))
				.setProjectType(NumberUtil.safeInt(item.getProjectType()))
				.setProjectItemId(NumberUtil.safeInt(item.getId()))
				.setUserid(NumberUtil.safeInt(item.getEntrusteeId()))
				.setUserType(CrowdsourceUserType.STUDIO)
				.setProjectAmount(item.getProjectAmount())
				.setCrowdsourceStepped(company.getCrowdsourceStepped())
				.setBaseTaxRate(company.getBaseTaxRate())
				.setLowTaxRate(company.getLowTaxRate())
				.setHighTaxRate(company.getHighTaxRate())
				.setDepositRate(company.getDepositRate())
				.setDepositFlag(company.getDepositFlag())
				.setPartnerBrokerageRate(partnerBrokerageRate);
		return model;
	}
}
